<template>
  <div>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px" class="ruleForm">
      <el-form-item label="引流规则">
        <el-select v-model="ruleForm.flow_rule_id" clearable filterable remote placeholder="请选择引流规则" style="width:100%;">
          <el-option v-for="(item,index) in selectList" :key="index" :label="item.label+' / '+item.value" :value="Number(item.value)"></el-option>
        </el-select>
        <div class="redText">配置后会显示，不配置不显示引流</div>
      </el-form-item>
    </el-form>
    <span class="dialog-footer rowFlex">
      <el-button class="btn" @click="close">取 消</el-button>
      <el-button class="btn" type="primary" @click="submitForm">确 定</el-button>
    </span>
  </div>
</template>
<script>
import { mapState, mapMutations, mapActions } from 'vuex'
import { flowRule } from '@/api/channel'
export default {
  props: {
    rowInfo: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      ruleForm: {
        flow_rule_id: ''
      },
      selectList: [],
      rules: {
        flow_rule_id: [
          { required: true, message: '请选择引流规则', trigger: 'change' }
        ]
      }
    }
  },
  async created() {
    this.resize_commonDataList({ type: 'flowRuleDown' })
    this.selectList = await this.requestCommonData({ type: 'flowRuleDown', mini_app_id: window.localStorage.getItem('mini_app_id') })
  },
  mounted() {
    if (this.rowInfo.flow_rule_id && this.rowInfo.flow_rule_id != 0) {
      this.ruleForm.flow_rule_id = Number(this.rowInfo.flow_rule_id)
    }
  },
  methods: {
    ...mapMutations('common', ['resize_commonDataList']),
    ...mapActions('common', ['requestCommonData']),
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.flowRule()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async flowRule() {
      const data = {
        channel_id: this.rowInfo.id,
        flow_rule_id: this.ruleForm.flow_rule_id
      }
      const res = await flowRule(data)
      if (res.status_code == 1) {
        this.$message.success(res.msg)
        this.$emit('ok')
      }
    },
    close() {
      this.$emit('cancel')
    }
  }
}
</script>

<style lang="scss" scoped>
.ruleForm{
    padding: 20px;
}
</style>